package com.item.read.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.item.read.ContentAction;
import com.item.read.store.SettingState;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AutoPageAction extends AnAction {

    public static volatile boolean flag = false;

    private static ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        if(!flag){
            Long interval = SettingState.getInstance().getConfig().getInterval();
            if(interval <= 0){
                interval = 8l;
            }
            interval*=1000;
            flag = true;
            int finalInterval = Math.toIntExact(interval);

            Thread thread = new Thread(){
                @Override
                public void run() {
                    while (flag) {
                        try {
                            Thread.sleep(finalInterval);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                        if(flag)
                            ContentAction.next();
                    }
                }
            };
            executorService.submit(thread);
         //   thread.start();
        }else{
            flag = false;
        }
    }


}
